﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using XMK.Tadong.Biz;
using XMK.Tadong.Model;
using System.IO;
using XMK.Utils;

namespace XMK.Tadong.Web.Handlers
{
    /// <summary>
    /// FileExistHandler 的摘要说明
    /// </summary>
    public class FileExistHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string resultJson = "{\"success\":true, \"exist\":true}";
            string fileId = context.Request.QueryString["fileid"];
            string spaceType = context.Request.Params.Get("space_type");
            ExtTypeinfo extcode = null;
            EXTBiz extbiz = new EXTBiz();
            XMKFileExtInfo _xmkFileExtInfoModel = null;

            try
            {
                if (string.IsNullOrEmpty(fileId))
                    resultJson = "{\"success\":false, \"exist\":true}";
               
                else
                { 
                    if (spaceType.Equals("30"))
                    {
                        Tb_Xmk_FileBiz biz = new Tb_Xmk_FileBiz();
                        _xmkFileExtInfoModel = biz.GetXMKFileExtInfoByID(fileId);
                        //判断文件是否为可转换文件，如果不是（如cdr），则直接返回ture
                        extcode = extbiz.getExtCodeByExtent(_xmkFileExtInfoModel.FILE_EXT);

                    }
                    else
                    {
                        TB_TEAM_FILEBiz teamBiz = new TB_TEAM_FILEBiz();
                        _xmkFileExtInfoModel = teamBiz.GetModelByFileId(fileId);
                        //判断文件是否为可转换文件，如果不是（如cdr），则直接返回ture
                        extcode = extbiz.getExtCodeByExtent(_xmkFileExtInfoModel.FILE_EXT);
                        
                    }
                    if (extcode == null)
                    {
                        resultJson = "{\"success\":true, \"exist\":true}";
                    }
                    else if ((extcode.EXT_CODE != "word" && extcode.EXT_CODE != "excel" && extcode.EXT_CODE != "ppt" && extcode.EXT_CODE != "pdf" && extcode.EXT_CODE != "txt"))
                    {
                        resultJson = "{\"success\":true, \"exist\":true}";
                    }
                    else if (_xmkFileExtInfoModel != null)
                    {
                        if (string.IsNullOrWhiteSpace(_xmkFileExtInfoModel.SwfFilePath))
                            resultJson = "{\"success\":true, \"exist\":false}";
                    }
                    else
                    {
                        resultJson = "{\"success\":true, \"exist\":false}";
                    }
                }
            }
            catch (Exception ex)
            {
                resultJson = "{\"success\":false, \"exist\":false}";
                LogHelper.WriteLog(LogType.Error, "验证文件是否存在异常," + ex.ToString());
            }

            
            
            context.Response.ContentType = "text/json";
            context.Response.Write(resultJson);
            context.Response.End();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}